﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:rep="urn:report-extended-utilities"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl rep">
  <xsl:output method="xml" indent="yes"/>
  <xsl:param name="report-name">SharePoint Generic Report</xsl:param>
  <xsl:param name="report-chart-type">Line</xsl:param>
  <xsl:param name="report-topN">10</xsl:param>
  <xsl:param name="report-date" />
  <xsl:param name="report-last-date" />
  <xsl:template match="/">
    <xsl:variable name="report-legend-title">Site Usage</xsl:variable>
    <Report Name="{$report-name}" ChartType="{$report-chart-type}" LegendTitle="{$report-legend-title}">
      <Filters>
        <Filter Name="All Web Applications" FilterAttribute="Storage" FilterValue="ALL" />
        <xsl:for-each select="//WebApplications/WebApplication">
          <Filter Name="Site col. - {@Url}" FilterAttribute="Storage" FilterValue="{@Id}" />
        </xsl:for-each>
      </Filters>
      <Series>
        <xsl:variable name="TotalUsageStorage" select="sum(//WebApplications/WebApplication/Sites/Site/Usage/@Storage)"/>
        <xsl:for-each select="//WebApplications/WebApplication">
          <xsl:variable name="WebAppId" select="@Id" />
          <xsl:variable name="WebAppUrl" select="@Url" />
          <xsl:variable name="UsageStorage" select="round(sum(Sites/Site/Usage/@Storage) div 1048576 * 100) div 100"/>
          <Serie Name="{$WebAppUrl}">
            <Item Name="{@Url}" Value="{$UsageStorage}" Storage="ALL" Date="{$report-date}" />
          </Serie>
        </xsl:for-each>
        <xsl:for-each select="//WebApplications/WebApplication">
          <xsl:variable name="CurWebAppId" select="@Id"/>
          <xsl:variable name="CurWebAppUrl" select="@Url"/>
          <xsl:for-each select="Sites/Site">
            <xsl:sort order="descending" select="Usage/@Storage"/>
            <xsl:if test="position() &lt;= $report-topN">
              <xsl:variable name="UsageStorage" select="round(Usage/@Storage div 1048576 * 100) div 100" />
              <xsl:variable name="SiteRelativeUrl">
                /<xsl:value-of select="substring-after(@Url, $CurWebAppUrl)" />
              </xsl:variable>
              <Serie Name="{$SiteRelativeUrl}">
                <Item Name="{$SiteRelativeUrl}" Value="{$UsageStorage}" Storage="{$CurWebAppId}" Date="{$report-date}" />
              </Serie>
            </xsl:if>
          </xsl:for-each>
        </xsl:for-each>
      </Series>
    </Report>
  </xsl:template>
</xsl:stylesheet>
